package com.heima.user.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.admin.dtos.ApUserRealname;
import com.heima.model.admin.dtos.AuthDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.user.mapper.ApUserRealNameMapper;
import com.heima.user.service.ApUserRealNameService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Transactional
@Slf4j
public class ApUserRealNameServiceImpl extends ServiceImpl<ApUserRealNameMapper, ApUserRealname>implements ApUserRealNameService {

    /**
     * 查询所有用户
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult findAll(AuthDto dto) {
        //检查参数
        if(dto==null)return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);

        dto.checkParam();
        //分页查询
        Page page = new Page();
        LambdaQueryWrapper<ApUserRealname> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //按照状态查询
        if(dto.getStatus()!=null){
            lambdaQueryWrapper.eq(ApUserRealname::getStatus,dto.getStatus());
        }
        //按照创建时间倒序排序
        lambdaQueryWrapper.orderByDesc(ApUserRealname::getCreatedTime);
        //返回结果
        page=page(page,lambdaQueryWrapper);
        ResponseResult result= new PageResponseResult(dto.getPage(),dto.getSize(),(int)page.getTotal());
        result.setData(page.getRecords());
        return result;
    }

    /**
     * 审核失败
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult authFail(AuthDto dto) {
        //检查参数
        if (dto==null)return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        ApUserRealname apUserRealname = new ApUserRealname();
        BeanUtils.copyProperties(dto,apUserRealname);
        //设置状态 status状态 0 创建中 1 待审核 2 审核失败  9 审核通过
        if(dto.getStatus()==null){
            apUserRealname.setStatus((short)2);
        }
        //设置审核失败理由
        if(StringUtils.isBlank(dto.getMsg())){
            apUserRealname.setReason("审核失败");
        }else {
            apUserRealname.setReason(dto.getMsg());
        }
        apUserRealname.setCreatedTime(new Date());
        updateById(apUserRealname);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 审核成功
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult authPass(AuthDto dto) {
        //检查参数
        if (dto==null)return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        ApUserRealname apUserRealname = new ApUserRealname();
        BeanUtils.copyProperties(dto,apUserRealname);
        //设置状态 status状态 0 创建中 1 待审核 2 审核失败  9 审核通过
        if(dto.getStatus()==null){
            apUserRealname.setStatus((short)9);
        }
        apUserRealname.setCreatedTime(new Date());
        updateById(apUserRealname);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);

    }
}
